/*
 * Created on 18. sep.. 2006
 * Created by Tomas Ekeli
 */
package util;


import java.util.Iterator;
import java.util.List;

import junit.framework.TestCase;

import org.junit.*;

import people.Group;
import people.MembershipManager;
import people.Person;
import placing.Mapper;
import placing.Room;
import placing.RoomManager;


public class FileHandlerTest extends TestCase{
	private MembershipManager members;
	private RoomManager rooms;
	private Mapper map, oldMap;
	private FileHandler fHandler;
	
	private Person frodo, sam, pippin, merry, aragorn, legolas, sauron, sarumann,
	gollum;
	private Group brotherhood, hobbits, evil;
	
	private String roomsFile 	= "files/testRooms.xml";
	private String personsFile 	= "files/testPersons.xml";
	private String groupsFile 	= "files/testGroups.xml";
	private String mapFile 		= "files/testMap.xml";
	
	@Before
	public void setUp() throws Exception {
		members = new MembershipManager();
		rooms = new RoomManager();
		map = new Mapper();
		
		frodo 	= new Person(1, "Frodo Baggins", 
				"frodo.b@baggins.org", "ringbearer", null);
		sam 	= new Person(2, "Sam Gamgee", 
				"samg@baggins.org", "likes boys", null);
		pippin 	= new Person(3, "Peregrin Took",
				"sexxybst@hotmail.com", "fun fun", null);
		merry	= new Person(4, "Meriadoc Brandybuck", 
				"mb@shire.com", "Will never amount to much", null);
		aragorn = new Person(5, "Aragorn II",
				"king@gondor.go", "The King", null);
		legolas = new Person(6, "Legolas Greenleaf",
				"woodlandlover@ithilien.com", "tall and blonde", null);
		sauron	= new Person(7, "Sauron the Dark",
				"boss@mordor.mo", "tall and dark", null);
		gollum 	= new Person(8, "Smeagol", 
				null, "creepy", null);
		sarumann = new Person(9, "Sarumann the White", 
				"Sarumann@orthanc.org", "Might become a problem", null);
		
		brotherhood = new Group("The Brotherhood of the Ring", 10);
		hobbits = new Group("The Hobbits of the Shire", 5);
		evil = new Group("The Dark Foe", 18);
		
		brotherhood.addPerson(frodo);
		brotherhood.addPerson(sam);
		brotherhood.addPerson(pippin);
		brotherhood.addPerson(merry);
		brotherhood.addPerson(aragorn);
		brotherhood.addPerson(legolas);
		
		hobbits.addPerson(frodo);
		hobbits.addPerson(sam);
		hobbits.addPerson(pippin);
		hobbits.addPerson(merry);
		
		evil.addPerson(sauron);
		evil.addPerson(gollum);
		
	}
	
	 
	 @Test
	 public void testWritingMap() throws Exception {
		map.addGroup(brotherhood);
		map.addGroup(hobbits);
		map.addGroup(evil);
		map.addPerson(sarumann);
		
		Room one = map.addRoom(2);
		Room two = map.addRoom(5);
		Room three = map.addRoom(1);
		Room four = map.addRoom(7);
		Room five = map.addRoom();
		
		map.reserveRoom(four, evil);
		map.reserveRoom(two, brotherhood);
		map.reservePlaceInRoom(one, hobbits);
		
		map.distributePersonsToPlaces();
		map.distributePersonsToPlaces();
		map.distributePersonsToPlaces();
		map.distributePersonsToPlaces();
		map.distributePersonsToPlaces();
		map.distributePersonsToPlaces();
		
		fHandler = new FileHandler(mapFile);
		
		fHandler.writeMap(map);
		
		oldMap = map;
	}
	 
	 
	 @Test
	 public void testReadingMap() throws Exception {
		 testWritingMap();
		fHandler = new FileHandler(mapFile);
		
		map = fHandler.readMap();
		
		assertEquals(oldMap.numberOfFreePlaces(), map.numberOfFreePlaces());
		assertEquals(oldMap.numberOfPersons(), map.numberOfPersons());
		assertEquals(oldMap.numberOfRooms(), map.numberOfRooms());
		assertEquals(oldMap.numberOfPlaces(), map.numberOfPlaces());
		
		List<Person> oldList = oldMap.getPersons();
		List<Person> newList = map.getPersons();
		Iterator<Person> it = newList.iterator();
		for (Person p : oldList) {
			System.out.println(p + "\n" + it.next());
		}
		
//		System.out.println("Persons " + map.numberOfPersons());
//		System.out.println("Places " + map.numberOfPlaces());
//		System.out.println("Rooms " + map.numberOfRooms());
//		System.out.println("Free places " + map.numberOfFreePlaces());
		
	}

}
